package edu.rit.hvz.android;

import edu.rit.hvz.android.DataLayer.MissionDBAdapter;
import edu.rit.hvz.android.DataLayer.NewsDBAdapter;
import edu.rit.hvz.android.DataLayer.PlayerDBAdapter;
import edu.rit.hvz.android.DataLayer.ServerDatabaseConnection;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FilterQueryProvider;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class MissionFeed extends ListActivity {
	private Cursor mCursor;
	private MissionDBAdapter db = null;
	private SimpleCursorAdapter adapter;
	private EditText filterText = null;
	
	private static final int Delete_ID = Menu.FIRST;
	private static final int REQUEST_CODE = 1;
	private static final int ADD_MISSION = 2;
	private static final int REFRESH = 3;
	private static final int EDIT_NEWS = 4;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Utils.onActivityCreateSetTheme(this);
		setContentView(R.layout.mission_feed_layout);
		registerForContextMenu(getListView());	

		filterText = (EditText) findViewById(R.id.mission_search);
		filterText.addTextChangedListener(filterTextWatcher);
		
		findViewById(R.id.createMission).setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				addingMission();
			}        	
        });

		db = new MissionDBAdapter(this);
		db.open();
		// Put a managed wrapper around the retrieved cursor so we don't have to
		// worry about
		// requerying or closing it as the activity changes state.
		mCursor = db.getAllMissions();
		startManagingCursor(mCursor);

		// Now create a new list adapter bound to the cursor.
		// SimpleListAdapter is designed for binding to a Cursor.
		adapter = new SimpleCursorAdapter(this, // Context.
				android.R.layout.simple_list_item_1, // Specify the row template
														// to use (here, two
														// columns bound to the
														// two retrieved cursor
														// rows).
				mCursor, // Pass in the cursor to bind to.
				new String[] { MissionDBAdapter.TITLE }, // Array of cursor
															// columns to bind
															// to.
				new int[] { android.R.id.text1 }); // Parallel array of which
													// template objects to bind
													// to those columns.

		adapter.setFilterQueryProvider(new FilterQueryProvider() {

			public Cursor runQuery(CharSequence constraint) {
				String partialValue = constraint.toString();
				return db.filterMissionByName(partialValue);

			}
		});

		// Bind to our new adapter.
		setListAdapter(adapter);
	}

	private TextWatcher filterTextWatcher = new TextWatcher() {

		public void afterTextChanged(Editable s) {
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			adapter.getFilter().filter(s);

		}

	};
	
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    menu.add(0, Delete_ID, 0, "Delete");
	    menu.add(0, EDIT_NEWS, 0, "Edit");
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = new Intent().setClass(this, Mission.class);
		intent.putExtra(MissionDBAdapter.ROW_ID, id);
		this.startActivity(intent);

	}

	public void onRestoreInstanceState(Bundle saved) {
		super.onRestoreInstanceState(saved);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, ADD_MISSION, 0, "Create new mission");
		menu.add(0, REFRESH, 0, "Refresh");
		return super.onCreateOptionsMenu(menu);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case ADD_MISSION:
			addingMission();
			return true;
		case R.id.settings:
			this.startActivity(new Intent(this, Preferences.class));
			return true;
		case REFRESH:
    		ServerDatabaseConnection.populateMissions(this);
    		adapter.getCursor().requery();
		}
		return false;
	}

	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		mCursor.moveToPosition(info.position);
		long id = mCursor.getLong(mCursor.getColumnIndex(db.ROW_ID));
		switch (item.getItemId()) {
		case Delete_ID:
			db.open();
			db.deleteMissionServer(id);			
			db.close();
			ServerDatabaseConnection.populateMissions(this);
			adapter.getCursor().requery();		
			return true;
		case EDIT_NEWS:
			editMission(id);
			return true;
		}
		return super.onContextItemSelected(item);
	}
	
	public void editMission(long id) {
		Intent intent = new Intent().setClass(this, EditMission.class);
		intent.putExtra(NewsDBAdapter.ROW_ID, id);
		
		this.startActivityForResult(intent, REQUEST_CODE);
	}

	public void addingMission() {
		// Intent intent = new Intent().setClass(this, CreateMission.class);
		// startActivity(intent);
		// startActivityForResult(intent, REQUEST_CODE);

		this.startActivity(new Intent(this, EditMission.class));
	}

}
